﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MusicDatabase
{
    /*
     * Auteur:  Joris Molnár en Niels van Leusden
     * Datum:   31-1-2013
     * Omschrijving: Deze staat tussen de klasse Register, die nieuwe users aanmaakt
     *               en de tabel tbluser in de database. En maakt het daadwerkelijk
     *               mogelijk om een nieuwe user toe te voegen in de database, waarna 
     *               deze door login opgehaald kan worden.
     * 
     */
    public class User
    {
        private int userID;

        public int UserID
        {
            get { return userID; }
        }

        private string nick;
        private string pass;
        private string fname;
        private string lname;
        private string email;
        private string descr;
        private string date;
        private DataMode dataMode;

        public User()
        {
        }

        public User(int userID)
        {
            this.dataMode = DataMode.raadplegen;
            string[] userData = this.GetUser(userID);
            this.userID = userID;
            this.nick = userData[1];
            this.pass = userData[2];
            this.fname = userData[3];
            this.lname = userData[4];
            this.email = userData[5];
            this.descr = userData[6];
            this.date = userData[7];
        }

        /// <summary>
        /// Prepare to add a user
        /// </summary>
        /// <param name="userData"></param>
        public User(string[] userData)
        {
            this.dataMode = DataMode.toevoegen;
            this.userID = -1;
            this.nick = userData[1];
            this.pass = userData[2];
            this.fname = userData[3];
            this.lname = userData[4];
            this.email = userData[5];
            this.descr = userData[6];
            this.date = userData[7];
        }

        /// <summary>
        /// Return user information
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        private string[] GetUser(int userID)
        {
            SQLServices sqlService = new SQLServices();
            string command = "SELECT * " +
                "FROM user " +
                "WHERE user_id = " + userID;
            return sqlService.GetTable(command)[1];
        }

        /// <summary>
        /// See if the username is in the database and if the password matches the saved one
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="nick"></param>
        /// <returns></returns>
        public bool Login(string userName, string password, ref string nick)
        {
            SQLServices sqlService = new SQLServices();
            string command = "SELECT user_nickname, user_password " +
                "FROM tbluser " +
                "WHERE LOWER(user_nickname) = '" + userName.ToLower() + "'";
            List<string[]> namepass = sqlService.GetTable(command);
            if (namepass.Count() == 2)
            {
                nick = namepass[1][0];
                return (namepass[1][1] == password);
            }
            else
            {
                return false;
            }
        }

        public string InsertCommand(string tabelnaam, string veldnamen, string velden)
        {
            return "INSERT INTO " + tabelnaam + "  (" + veldnamen + ") VALUES (" + velden + ") ";
        }

        private string veldnamen = "user_nickname, user_password, user_firstname, user_lastname, user_email, user_descr, user_created";

        /// <summary>
        /// in geval van een nieuwe artist (dataMode == toevoegen):
        /// -- controle of de artist nieuw is
        /// -- toevoegen artist
        /// -- ophalen artist voor het toegekende "artistID"
        /// </summary>
        /// <param name="dm">DataMode: Geeft aan op welke wijze het object gesaved moet worden.</param>
        public void Save()
        {
            SQLServices sqlService = new SQLServices();
            if (this.dataMode == DataMode.toevoegen && !this.Exist(sqlService))
            {
                string velden = "'" + nick + "','" + pass + "','" + fname.Replace("'", "''") + "','" + lname.Replace("'", "''") + "','" + email + "','" + descr.Replace("'", "''") + "','" + date + "'";
                string command = InsertCommand("TBLUSER", veldnamen, velden);
                sqlService.Insert(command);
                Exist(sqlService); // haalt het record opnieuw op voor het UserID...
            }
            if (this.dataMode == DataMode.toevoegen)
            {
                userID = Convert.ToInt32(sqlService.GetVeldValue("user_id"));
            }
            this.dataMode = DataMode.raadplegen;
        }

        /// <summary>
        /// raadpleegt het record op basis van identificerende gegevens naam+geboortedatum
        /// </summary>
        /// <param name="sqlService"></param>
        /// <returns></returns>
        private bool Exist(SQLServices sqlService)
        {
            string command = "SELECT * FROM tbluser WHERE user_nickname = '" + nick + "'";
            //string command = SelectCommand("ARTIST", " WHERE art_name = '" + name + "' AND art_formed_date = '" + this.formedDate + "'");
            return (sqlService.Exist(command));
        }
    }
}
